from app.api.services.translators.baidu.baidu_translate import BaiduTranslatorV2
from app.api.services.translators.edge.edge_translate import EdgeTranslator
from app.api.services.translators.abstract_translate import AbstractTranslator
from app.api.schemas import Lang, TextTransResult


class TranslateClient:
    """
    翻译工厂
    """

    # 翻译器映射
    TRANS = {
        "baidu": BaiduTranslatorV2(),
        "edge": EdgeTranslator(),
    }
    # 是否使用代理
    USE_PROXY = True

    # 默认引擎类型
    DEF_ENGINE_TYPE = "baidu"

    @staticmethod
    def get_translator(type: str) -> AbstractTranslator:
        """
        获取翻译器
        """
        return TranslateClient.TRANS.get(type, TranslateClient.TRANS["baidu"])

    @staticmethod
    def translate_text(text: str, from_lang: str, to_lang: str) -> TextTransResult:
        """
        翻译文本
        """
        translator = TranslateClient.get_translator(TranslateClient.DEF_ENGINE_TYPE)
        return translator.translate(
            text,
            Lang.of(from_lang),
            Lang.of(to_lang),
            use_proxy=TranslateClient.USE_PROXY,
        )
